package com.cn.wanxi.util;

import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Tool {

    /**
     *
     * @param value
     * @return
     */
    public static Integer strToInt(String value) {

        try {
            return Integer.valueOf(value);
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     *
     * @param value
     * @return
     */
    public static Integer strToPage(String value) {

        try {
            return Integer.valueOf(value);
        } catch (Exception e) {
            return 1;
        }
    }

    /**
     *
     * @param value
     * @return
     */
    public static Integer strToLimit(String value) {

        try {
            return Integer.valueOf(value);
        } catch (Exception e) {
            return 10;
        }
    }
    public static String encodeByMD5(String password) {
        try {
            //确定计算方法
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            BASE64Encoder base64en = new BASE64Encoder();
            //加密后的字符串
            return base64en.encode(md5.digest(password.getBytes("utf-8")));

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }
}
